import request from '@/utils/request';
import { requestWrapper } from '@/utils/mock';

// 模拟数据
const mockApiDocList = () => ({
  list: [
    {
      docId: 1,
      title: '用户管理接口文档',
      description: '系统用户管理相关API接口',
      type: 'Swagger',
      url: '/swagger/user-api.json',
      content: '', // 模拟数据可以为空，实际预览时通过url加载
      status: '1',
      createTime: '2023-01-01 10:00:00',
      updateTime: '2023-01-02 11:00:00'
    },
    {
      docId: 2,
      title: '角色管理接口文档',
      description: '系统角色管理相关API接口',
      type: 'OpenAPI',
      url: '',
      content: '{\"openapi\":\"3.0.0\",\"info\":{\"title\":\"Role API\",\"version\":\"1.0\"}}',
      status: '0',
      createTime: '2023-01-05 14:00:00',
      updateTime: '2023-01-05 14:00:00'
    }
  ],
  total: 2,
  pageNum: 1,
  pageSize: 10
});

// 获取API文档列表
export function getApiDocList(params) {
  return requestWrapper(
    () => request({
      url: '/system/api-doc/list',
      method: 'get',
      params
    }),
    mockApiDocList
  );
}

// 模拟数据
const mockApiDocDetail = (docId) => {
  const list = mockApiDocList().list;
  return list.find(item => item.docId === docId) || null;
};

// 获取API文档详情
export function getApiDocDetail(docId) {
  return requestWrapper(
    () => request({
      url: `/system/api-doc/${docId}`,
      method: 'get',
    }),
    () => mockApiDocDetail(docId)
  );
}

// 新增API文档
export function addApiDoc(data) {
  return requestWrapper(
    () => request({
      url: '/system/api-doc',
      method: 'post',
      data
    }),
    { success: true, message: '新增成功' }
  );
}

// 修改API文档
export function updateApiDoc(data) {
  return requestWrapper(
    () => request({
      url: '/system/api-doc',
      method: 'put',
      data
    }),
    { success: true, message: '修改成功' }
  );
}

// 删除API文档
export function deleteApiDoc(docIds) {
  return requestWrapper(
    () => request({
      url: `/system/api-doc/${docIds}`,
      method: 'delete',
    }),
    { success: true, message: '删除成功' }
  );
}

// 更新API文档状态
export function updateApiDocStatus(data) {
  return requestWrapper(
    () => request({
      url: `/system/api-doc/status`,
      method: 'put',
      data
    }),
    { success: true, message: '状态更新成功' }
  );
}

// 导入Swagger/OpenAPI文档
export function importApiDoc(file) {
  const formData = new FormData();
  formData.append('file', file);
  return requestWrapper(
    () => request({
      url: '/system/api-doc/import',
      method: 'post',
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }),
    { successCount: 1, failCount: 0, message: '导入成功' }
  );
}

// 导出API文档
export function exportApiDoc(docId) {
  return requestWrapper(
    () => request({
      url: `/system/api-doc/export/${docId}`,
      method: 'get',
      responseType: 'blob' // 导出文件流
    }),
    new Blob(["mock export content"], { type: "text/plain" }) // 模拟文件内容
  );
}

// 获取API文档分类
export function getApiDocCategories() {
  return requestWrapper(
    () => request({
      url: '/system/api-doc/categories',
      method: 'get',
    }),
    { 
      list: [
        { id: 1, name: '用户管理' },
        { id: 2, name: '角色管理' },
        { id: 3, name: '认证授权' },
        { id: 4, name: '系统监控' }
      ]
    }
  );
}

// API文档访问和展示（此处仅定义，实际前端不直接调用，而是通过iframe等方式加载）
export const getSwaggerUiHtml = (params) => {
  return requestWrapper(
    () => request({
      url: '/tool/api-doc/swagger-ui.html',
      method: 'get',
      params
    }),
    `<html><body><h1>模拟Swagger UI HTML</h1></body></html>`
  );
};

export const getSwaggerConfig = (params) => {
  return requestWrapper(
    () => request({
      url: '/tool/api-doc/swagger-config',
      method: 'get',
      params
    }),
    {}
  );
};

export const getSwaggerResources = (params) => {
  return requestWrapper(
    () => request({
      url: '/tool/api-doc/swagger-resources',
      method: 'get',
      params
    }),
    []
  );
};

export const getOpenApiJson = (params) => {
  return requestWrapper(
    () => request({
      url: '/tool/api-doc/v3/api-docs',
      method: 'get',
      params
    }),
    {}
  );
};

export const getOpenApiYaml = (params) => {
  return requestWrapper(
    () => request({
      url: '/tool/api-doc/v3/api-docs.yaml',
      method: 'get',
      params
    }),
    `---
`
  );
};

export const getOpenApiJsonFormat = (params) => {
  return requestWrapper(
    () => request({
      url: '/tool/api-doc/v3/api-docs.json',
      method: 'get',
      params
    }),
    {}
  );
};

export const getOpenXml = (params) => {
  return requestWrapper(
    () => request({
      url: '/tool/api-doc/v3/api-docs.xml',
      method: 'get',
      params
    }),
    `<root></root>`
  );
};

export const getOpenApiHtml = (params) => {
  return requestWrapper(
    () => request({
      url: '/tool/api-doc/v3/api-docs.html',
      method: 'get',
      params
    }),
    `<html><body><h1>模拟v3 API Docs HTML</h1></body></html>`
  );
};

export const getOpenApiMarkdown = (params) => {
  return requestWrapper(
    () => request({
      url: '/tool/api-doc/v3/api-docs.md',
      method: 'get',
      params
    }),
    `# API文档
## 接口列表
...`
  );
};

export const getOpenApiText = (params) => {
  return requestWrapper(
    () => request({
      url: '/tool/api-doc/v3/api-docs.txt',
      method: 'get',
      params
    }),
    `API文档文本格式...`
  );
};

export const getOpenApiCsv = (params) => {
  return requestWrapper(
    () => request({
      url: '/tool/api-doc/v3/api-docs.csv',
      method: 'get',
      params
    }),
    `路径,方法,描述\n/user,GET,获取用户列表`
  );
}; 